﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Common.XmlConfig;
using System.Web.Caching;

namespace ZWZ.Web.Config
{
    public class ConfigAdapter
    {
        static Dictionary<string, string> CMgrsFilePath
        {
            get
            {
                var result = new Dictionary<string, string>();
                result.Add("SiteSettingMgr", "~/Config/xml/SiteSetting.xml");
                result.Add("SiteDataMgr", "~/Config/xml/SiteData.xml");
                result.Add("SitePower", "~/Config/xml/SitePower.xml");
                result.Add("AdminHelp", "~/WebAdmin/HelpInfo/WebSiteHelp.xml");
                result.Add("AdminMenu", "~/WebAdmin/m.xml");
                return result;
            }
        }
        static Dictionary<string, ConfigMgr> CMgrs;
        static Dictionary<string, CacheDependency> CMgrsDep;
        public static void Init()
        {
            CMgrs = new Dictionary<string, ConfigMgr>();
            CMgrsDep = new Dictionary<string, CacheDependency>();

            foreach (var file in CMgrsFilePath)
            {
                string filepath = HttpContext.Current.Server.MapPath(file.Value);
                CMgrsDep.Add(file.Key, new CacheDependency(filepath));
                CMgrs.Add(file.Key, new ConfigMgr(filepath));
            }
        }
        public static ConfigMgr GetManager(string key)
        {
            if (CMgrs == null) { Init(); }
            var dep = CMgrsDep[key] as CacheDependency;
            var file = CMgrsFilePath[key].ToString();
            if (dep.HasChanged)
            {
                CMgrs[key] = new ConfigMgr(HttpContext.Current.Server.MapPath(file));
            }
            return CMgrs[key] as ConfigMgr;
        }
        public static ConfigMgr SiteSettingMgr { get { return GetManager("SiteSettingMgr"); } }
        public static ConfigMgr SiteDataMgr { get { return GetManager("SiteDataMgr"); } }
        public static ConfigMgr AdminHelper { get { return GetManager("AdminHelp"); } }
        public static ConfigMgr AdminMenu { get { return GetManager("AdminMenu"); } }
        public static ConfigMgr SitePower { get { return GetManager("SitePower"); } }
        public static string GetHtml(ConfigSetting setting)
        {
            string Id = "WebCfg$" + setting.ParentGroup.Key + "$" + setting.Key;
            switch (setting.Type)
            {
                
                case SettingType.Bool:
                    return string.Format("<select id='{0}' name='{0}' class='{2}'  value='{1}'><option value='True'>是</option><option value='False'>否</option></select><script type='text/javascript'> document.getElementById('{0}').value='{1}'</script>", Id, setting.Value, setting.Type);
                case SettingType.Text:
                case SettingType.Date:
                case SettingType.Email:
                case SettingType.Number:
                case SettingType.Range:
                case SettingType.Url:
                    return string.Format("<input id=\"{0}\" name=\"{0}\" class='{2}' type=\"text\" value=\"{1}\" />", Id, setting.Value, setting.Type);
                case SettingType.Password:
                    return string.Format("<input id=\"{0}\" name=\"{0}\" class='{2}'  type=\"password\" value=\"{1}\" />", Id, setting.Value, setting.Type);
                case SettingType.Image:
                    return string.Format("<img alt=\"{0}\" src=\"{1}\" class='{2}'  style=\"max-height: 100px; max-width: 100%;\"><br><input  runat=\"server\"  id=\"{2}\" name=\"{2}\" type=\"file\" />", 
                        setting.Text, setting.Value, Id, setting.Type);
                case SettingType.File:
                case SettingType.Flash:
                case SettingType.Video:
                    return string.Format("<input  runat=\"server\"  id=\"{0}\" name=\"{0}\" type=\"file\" />",Id);
                case SettingType.MultText:
                case SettingType.Html:
                    return string.Format("<textarea id='{0}' name='{0}' class='{2}' rows=\"3\" cols=\"20\">{1}</textarea>", Id, setting.Value, setting.Type);
                case SettingType.Select:
                    return string.Format("<select id='{0}' name='{0}' class='{2}'  value='{1}'><option value='True'>是</option><option value='False'>否</option></select><script type='text/javascript'> document.getElementById('{0}').value='{1}'</script>", Id, setting.Value, setting.Type);
                default:
                    return string.Format("<span id='{0}' class=\"{2}\"  name='{0}'>{1}</span>", Id, setting.Value, setting.Type);
            }
        }
    }
}